//@ts-nocheck
import {fromJS} from 'immutable';

/** 1.fromJS
 * 它的功能是将 JS 对象转换为 immutable 对象。*/
/*const immutableState = fromJS ({
  count: 0
});

console.log('immutableState:',immutableState);*/
/*
immutableState:
Map {size: 1, _root: ArrayMapNode, __ownerID: undefined, __hash: undefined, __altered: false}
size: 1
_root: ArrayMapNode {ownerID: OwnerID, entries: Array(1)}
__ownerID: undefined
__hash: undefined
__altered: false
__proto__: KeyedCollection
*/


/** 2. toJS
 * 和 fromJS 功能刚好相反，用来将 immutable 对象转换为 JS 对象*/
/*const jsObj = immutableState.toJS ();

console.log('jsObj:', jsObj);*/
/*
jsObj:
{count: 0}
count: 0
__proto__: Object
*/

/** 3.get/getIn
 * 用来获取 immutable 对象属性。
 * 通过与 JS 对象的对比来体会一下：*/

//JS 对象
const jsObj = {a: 1};
// const res = jsObj.a;
// console.log(res);

//immutable 对象
const immutableObj = fromJS (jsObj);
const res = immutableObj.get ('a');
console.log(res); // 1 ← 没错就是普通的1

/** .getIn*/
/*
//JS 对象
const jsObj2 = {a: {b: 1}};
// const res = jsObj2.a.b;

//immutable 对象
const immutableObj = fromJS (jsObj2);
const res = immutableObj.getIn (['a', 'b']);// 注意传入的是一个数组
console.log(res); // 1 ← 没错就是普通的1
*/


/** 4.set
 * 返回的是一个全新的对象
 * 用来对 immutable 对象的属性赋值。*/
const immutableObj = fromJS ({a: 1});
const new_immutableObj = immutableObj.set ('a', 2);
console.log(immutableObj.get('a')); // 1
console.log(new_immutableObj.get('a')); // 2


/** 5.merge
 * 返回的是一个全新的对象
 * 新数据与旧数据对比，旧数据中不存在的属性直接添加，旧数据中已存在的属性用新数据中的覆盖。*/
const immutableObj = fromJS ({a: 1});
const new_immutableObj = immutableObj.merge ({
  a: 2,
  b: 3
});// 修改了 a 属性,增加了 b 属性
console.log('new_immutableObj:',new_immutableObj);
/*
new_immutableObj:
Map {size: 2, _root: ArrayMapNode, __ownerID: undefined, __hash: undefined, __altered: false}
size: 2
_root: ArrayMapNode
  ownerID: OwnerID {}
  entries: Array(2)
    0: (2) ["a", 2]
    1: (2) ["b", 3]
  length: 2
  __proto__: Array(0)
__proto__: Object
__ownerID: undefined
__hash: undefined
__altered: false
__proto__: KeyedCollection
*/
console.log('immutableObj:',immutableObj);
/*
immutableObj:
Map {size: 1, _root: ArrayMapNode, __ownerID: undefined, __hash: undefined, __altered: false}
size: 1
_root: ArrayMapNode
  ownerID: OwnerID {}
  entries: Array(1)
    0: (2) ["a", 1]
    length: 1
  __proto__: Array(0)
__proto__: Object
__ownerID: undefined
__hash: undefined
__altered: false
__proto__: KeyedCollection
*/

